package com.powernode.springmvc.controller;

import com.powernode.springmvc.pojo.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Controller
public class UserController {
    @RequestMapping("/")
    public String toRegisterPage() {
        return "register";
    }

//    @PostMapping(value = "/register")
//    public String register(HttpServletRequest request) {
//        // 通过当前请求对象获取提交的数据
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String sex = request.getParameter("sex");
//        String[] hobbies = request.getParameterValues("hobby");
//        String intro = request.getParameter("intro");
//        System.out.println(username + "," + password + "," + sex + "," + Arrays.toString(hobbies) + "," + intro);
//        return "success";
//    }

//    /**
//     * 使用RequestParam获取参数
//     *
//     * @return
//     */
//    @PostMapping(value = "/register")
//    public String register(@RequestParam(value = "username") String a, @RequestParam(value = "password") String b,
//                           @RequestParam(value = "sex") String c, @RequestParam(value = "hobby") String[] d,
//                           @RequestParam(name = "intro") String e, @RequestParam(name = "age", required = false, defaultValue = "0") Integer age) {
//        System.out.println(a);
//        System.out.println(b);
//        System.out.println(c);
//        System.out.println(Arrays.toString(d));
//        System.out.println(e);
//        System.out.println(age);
//        return "success";
//    }

//    /**
//     * 直接使用形参获取参数
//     * @return
//     */
//    @PostMapping(value = "/register")
//    public String register(String username, String password, String sex, String[] hobby, String intro, Integer age) {
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(sex);
//        System.out.println(Arrays.toString(hobby));
//        System.out.println(intro);
//        System.out.println(age);
//        return "success";
//    }

    /**
     * 直接使用JavaBean类获取参数
     *
     * @return
     */
//    @PostMapping(value = "/register")
    @RequestMapping(value = "/register", method = {RequestMethod.POST, RequestMethod.GET})
    public String register(UserInfo userInfo, @RequestHeader(name = "referer", required = false, defaultValue = "") String referer,
                           @CookieValue(value = "id", required = false, defaultValue = "") String id) {
        System.out.println(userInfo.getUsername());
        System.out.println(userInfo.getPassword());
        System.out.println(userInfo.getPassword());
        System.out.println(Arrays.toString(userInfo.getHobby()));
        System.out.println(userInfo.getIntro());
        System.out.println(userInfo.getAge());
        System.out.println("referer: " + referer);
        System.out.println("CookieValue:id: " + id);
        return "success";
    }
}
